package cn.bonoon.kernel.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


/**
 * <pre>
 * 具体的业务系统的所有文件的上传，都应该通过本接口来取得读取或保存文件的操作流或者打开文件句柄；
 * 文件的存储策略则应该的可控的，也就是说可以通过其它插件(如：虚拟文件系统插件)来管理具体文件的
 * 上传及读取操作。
 * </pre>
 * @author jackson
 *
 */
public interface FileManager {
	
	/**
	 * <pre>
	 * 这里列出的不应该是服务器上的根目录；而是已经设定的系统存放上传文件的目录
	 * </pre>
	 * @return
	 */
	File[] listRoots();
	
	/**
	 * 下载文件
	 * @param request
	 * @param response
	 * @param path
	 * @param name
	 * @param ext
	 * @throws Exception
	 */
	void loadFile(HttpServletRequest request, HttpServletResponse response, String path, String name, String ext) throws Exception;
	
	/**
	 * 直接在浏览器上打开文件
	 * @param request
	 * @param response
	 * @param path
	 * @param name
	 * @param ext
	 * @throws Exception
	 */
	void openFile(HttpServletRequest request, HttpServletResponse response, String path, String name, String ext) throws Exception;
	/**
	 * 打开一个可写入文件的写入流
	 * @param path
	 * @return
	 */
	OutputStream openOutputStream(String path) throws FileNotFoundException;
	/**
	 * 打开一个可从文件读取内容的读取流
	 * @param path
	 * @return
	 */
	InputStream openInputStream(String path) throws FileNotFoundException;
	
	boolean delete(String path) throws Exception;
	
	File getFile(String path);
	
	/**
	 * 这种方式会随机重新生成上传的文件的名称
	 * @param dir
	 * @param file
	 * @return 保存后的文件的信息
	 */
	FileInfo save(String dir, MultipartFile file)throws IllegalStateException, IOException;
	FileInfo save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile file)throws IllegalStateException, IOException;
	
	FileInfo[] save(String dir, MultipartFile... files)throws IllegalStateException, IOException;
	FileInfo[] save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile... files)throws IllegalStateException, IOException;

	FileInfo save(String dir, MultipartHttpServletRequest multipartRequest, String filename)throws IllegalStateException, IOException;
	FileInfo save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartHttpServletRequest multipartRequest, String filename)throws IllegalStateException, IOException;
	
	/**
	 * 保存多个文件
	 * @param dir
	 * @param multipartRequest
	 * @param filenames
	 * @return
	 */
	FileInfo[] save(String dir, MultipartHttpServletRequest multipartRequest, String...filenames)throws IllegalStateException, IOException;
	FileInfo[] save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartHttpServletRequest multipartRequest, String...filenames)throws IllegalStateException, IOException;
}
